using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core.Model;


namespace TTTView
{
    public partial class frmTTTGame : Form
    {
        private Boxes boxes;
        private Box box;
        Player player1;
        Player player2;
        TicTacToe TTT;

        public frmTTTGame()
        {
            boxes = new Boxes();
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            foreach (TttButton btn in boxes)
            {
                btn.Image = Image.FromFile("..\\..\\images\\blank.gif");
                this.Controls.Add(btn);
            }
            player1 = new Player("Suryakant");
            player2 = new Player("Nirajan");

            TTT = new TicTacToe(player1, player2);
            
            boxes.onPlace += new BoxHandler(boxes_onPlace);
        }

        private void boxes_onPlace(Box box)
        {
            if (TTT.Turn == null || TTT.Turn != player1)
                TTT.PlaceAPiece(player1, box.Row, box.Col);
            else
                TTT.PlaceAPiece(player2, box.Row, box.Col);

            if (TTT.GameStatus == TicTacToe._GameState.PlayerWins)
            {
                MessageBox.Show(TTT.Winner.PlayerName + " wins");
            }
            else if (TTT.GameStatus == TicTacToe._GameState.GameDrawn)
            {
                MessageBox.Show("Game is drawn.");
                /*if (MessageBox.Show("Game is drawn. Would you like to play again.", "Tic Tac Toe", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Init();
                }*/
            }
        }
    }
}